var express = require('express')
var router = express.Router()
const login = require('../controller/user')
const { SuccessModel, ErrorModel } = require('../module/resModel')

/* GET users listing. */
router.post('/login', async function (req, res, next) {
  const { username, password } = req.body
  const res2 = await login(username, password)
  console.log(res2, '---- res2 ----')

  if (!res2) {
    res.json(new ErrorModel('username or password is wrong!'))
    return
  }
  console.log(req.session, '--- req.session 14 ---')

  req.session.username = username
  req.session.password = password
  res.json(new SuccessModel('login success!'))
})
// router.get('/session-test', function (req, res, next) {
//   if (req.session.views) {
//     req.session.views++
//     res.send(`You visited this page ${req.session.views} times`)
//   } else {
//     req.session.views = 1
//     res.send('Welcome to the session demo. Refresh!')
//   }
// })
router.get('/login-test', function (req, res, next) {
  if (req.session.username) {
    res.send(`登录成功~`)
  } else {
    res.send('你还没有登录哦~')
  }
})

module.exports = router
